/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.msk.sys.domain;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.msk.common.core.domain.model.LoginUser;
import com.msk.common.utils.IdGen;
import com.msk.common.utils.StringUtils;
import org.hibernate.validator.constraints.Length;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 数据Entity类
 *
 * @author ThinkGem
 * @version 2014-05-16
 */
public abstract class DataEntity<T> {

	private static final long serialVersionUID = 1L;
	protected String id; //主键
	protected String remarks; // 备注
	protected User createBy; // 创建者
	protected Date createDate; // 创建日期
	protected User updateBy; // 更新者
	protected Date updateDate; // 更新日期
	protected String delFlag; // 删除标记（0：正常；1：删除；2：审核）
	protected String nqId;//不等于id，用于唯一性校验
	protected Integer version;//数据版本，用于乐观锁的控制
	protected String orderBy;//用于排序使用
	/** 请求参数 */
	@JsonInclude(JsonInclude.Include.NON_EMPTY)
	private Map<String, Object> params;
	/**
	 * 删除标记（0：正常；1：删除；2：审核；）
	 */
	public static final String DEL_FLAG_NORMAL = "0";
	public static final String DEL_FLAG_DELETE = "1";

	public DataEntity() {
		super();
		this.delFlag = DEL_FLAG_NORMAL;
	}

	public DataEntity(String id) {
		this.id = id;
		this.delFlag = DEL_FLAG_NORMAL;
	}

	/**
	 * 插入之前执行方法，需要手动调用
	 */
	public void preInsert() {
		// 不限制ID为UUID，调用setIsNewRecord()使用自定义ID
		if (StringUtils.isEmpty(this.getId())) {
			setId(IdGen.uuid());
			if(!(StringUtils.isNotNull(this.getCreateBy())&&StringUtils.isNotBlank(this.getCreateBy().getId()))){
				this.createBy = new User("dddc3d5969014e298cb455e8f862eefd");
			}

		}

		if(!(StringUtils.isNotNull(this.getUpdateBy())&&StringUtils.isNotBlank(this.getUpdateBy().getId()))){
			this.updateBy = new User("dddc3d5969014e298cb455e8f862eefd");
		}

		this.version = 1;
		this.updateDate = new Date();
		if (this.createDate == null) {
			this.createDate = this.updateDate;
		}
	}

	/**
	 * 更新之前执行方法
	 */
	public void preUpdate() {
		if (StringUtils.isNotEmpty(this.getId())) {
			if (this.updateBy == null) {
				this.updateBy = new User("dddc3d5969014e298cb455e8f862eefd");
			}
			this.updateDate = new Date();
		}
	}

	/**
	 * 插入之前执行方法，需要手动调用
	 * @param loginUser
	 */
	public void preInsert(LoginUser loginUser) {
		if (StringUtils.isEmpty(this.getId())) {
			setId(IdGen.uuid());
			if (loginUser != null && StringUtils.isNotEmpty(loginUser.getLoginId())) {
				this.createBy = new User(loginUser.getLoginId());
			} else {
				this.createBy = new User("dddc3d5969014e298cb455e8f862eefd");
			}
			this.updateDate = new Date();
			if (this.createDate == null) {
				this.createDate = this.updateDate;
			}
		}
	}

	/**
	 * 更新之前执行方法
	 * @param loginUser
	 */
	public void preUpdate(LoginUser loginUser) {
		if (StringUtils.isNotEmpty(this.getId())) {
			if (loginUser != null && StringUtils.isNotEmpty(loginUser.getLoginId())) {
				this.updateBy = new User(loginUser.getLoginId());
			} else {
				this.updateBy = new User("dddc3d5969014e298cb455e8f862eefd");
			}
			this.updateDate = new Date();
		}
	}

	@Length(min = 0, max = 255)
	public String getRemarks() {
		return remarks;
	}

	public void setRemarks(String remarks) {
		this.remarks = remarks;
	}

	public User getCreateBy() {
		return createBy;
	}

	public void setCreateBy(User createBy) {
		this.createBy = createBy;
	}

	@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
	public Date getCreateDate() {
		return createDate;
	}

	public void setCreateDate(Date createDate) {
		this.createDate = createDate;
	}

	@JsonIgnore
	public User getUpdateBy() {
		return updateBy;
	}

	public void setUpdateBy(User updateBy) {
		this.updateBy = updateBy;
	}

	@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
	public Date getUpdateDate() {
		return updateDate;
	}

	public void setUpdateDate(Date updateDate) {
		this.updateDate = updateDate;
	}

	//@JsonIgnore
	@Length(min = 1, max = 1)
	public String getDelFlag() {
		return delFlag;
	}

	public void setDelFlag(String delFlag) {
		this.delFlag = delFlag;
	}


	public String getNqId() {
		return nqId;
	}


	public void setNqId(String nqId) {
		this.nqId = nqId;
	}


	public static long getSerialversionuid() {
		return serialVersionUID;
	}


	public Integer getVersion() {
		return version;
	}


	public void setVersion(Integer version) {
		this.version = version;
	}


	public String getOrderBy() {
		return orderBy;
	}


	public void setOrderBy(String orderBy) {
		this.orderBy = orderBy;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public Map<String, Object> getParams()
	{
		if (params == null)
		{
			params = new HashMap<>();
		}
		return params;
	}

	public void setParams(Map<String, Object> params)
	{
		this.params = params;
	}

	/**
	 * 获取数据库名称
	 */
	@JsonIgnore
	public String getDbName(){
		return "mysql";
	}
}
